//
//  CZNewFeatureController.swift
//  weibo27
//
//  Created by mac on 16/4/18.
//  Copyright © 2016年 it.cast. All rights reserved.
//

import UIKit

private let CZNewFeatureCellId = "CZNewFeatureCellId"

private let NewFeatureCount = 4

class CZNewFeatureController: UICollectionViewController {

    //使用懒加载的方式，是在控制器创建之后才开始的，
    private lazy var layout = UICollectionViewFlowLayout()
    
    init(){
        
        let layout = UICollectionViewFlowLayout()
        
        //这里的layout不能使用懒加载的方式，因为初始化的时候就要创建出来，而懒加载是在视图控制器创建之后才创建的
        super.init(collectionViewLayout: layout)
        
        layout.itemSize = kScreenSize
        
        layout.minimumInteritemSpacing = 0
        
        layout.minimumLineSpacing = 0
        
        collectionView?.dataSource = self
        
        layout.scrollDirection = .Horizontal
   
    }

    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Register cell classes
        self.collectionView!.registerClass(CZNewFeatureCell.self, forCellWithReuseIdentifier: CZNewFeatureCellId)
        
        //设置一些layout的一些基本属性
        collectionView?.bounces = false
        
        collectionView?.pagingEnabled = true
        
        collectionView?.showsHorizontalScrollIndicator = false
    
        collectionView?.backgroundColor = UIColor.whiteColor()
    }

    // MARK: UICollectionViewDataSource
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return NewFeatureCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(CZNewFeatureCellId, forIndexPath: indexPath) as! CZNewFeatureCell
    
        cell.imageIndex = indexPath.item
    
        return cell
    }

    
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        
        let page = Int(scrollView.contentOffset.x) / Int(kScreenW)
       
        if page != NewFeatureCount-1 {
            
            return
        }
        
        //拿到对应的cell
        let cell = collectionView?.cellForItemAtIndexPath(NSIndexPath(forItem: page, inSection: 0)) as! CZNewFeatureCell
        
        cell.startAnimation()
    }
    
    override func prefersStatusBarHidden() -> Bool {
        return true
    }
}















